package com.dave.gd.core.services.utils;

import java.io.Writer;

import com.dave.gd.common.exception.RtManagerException;
import com.dave.gd.common.util.JSONUtils;
import com.dave.gd.common.util.xml.XStreamUtils;

/**
 * Created by linzq on 2014/11/3.
 */
public class ParseUtils {
    public static String objectToSerial(Object obj, final String type) {
        if (ServConstants.SERVICE_SEARIL_TYPE_JSON.equals(type)) {
            return JSONUtils.toJsonString(obj);
        }
        try {
            //            Writer w = CastorUtils.objectToSerial(obj);
            Writer w = XStreamUtils.objectToSerial(obj);
            return w.toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RtManagerException("转换XML失败，失败原因：" + e.getMessage());
        }
    }
    
    /**
     * .
     * 
     */
    public static Object serialToObject(Class<?> clazz, String str, final String type) {
        if (ServConstants.SERVICE_SEARIL_TYPE_JSON.equals(type)) {
            return JSONUtils.jsonToObject(str, clazz);
        }
        //        return CastorUtils.serialToObject(str, clazz);
        return XStreamUtils.serialToObject(str, clazz);
    }
    
}
